#pragma once

#include<iostream>
#include <sys/epoll.h> 
#include<string.h>

#include"Log.hpp"

const int defaultEpollSize=1024;
class Epoll
{
public:
    Epoll()
    {
        _fd=epoll_create(defaultEpollSize);
        if(_fd<0)
        {
            Log(FATAL,"epoll create error\n");
            return;
        }
    }

    bool EpollAdd(int fd,uint32_t events)
    {
        struct epoll_event ee;
        bzero(&ee,sizeof(ee));
        ee.events=events;
        ee.data.fd=fd;
        if(epoll_ctl(_fd,EPOLL_CTL_ADD,fd,&ee)<0)
        {
            Log(ERROR,"epoll add error\n");
            return false;
        }

        return true;
    }

    bool EpollMod(int fd,uint32_t events)
    {
        struct epoll_event ee;
        bzero(&ee,sizeof(ee));
        ee.events=events;
        ee.data.fd=fd;
        if(epoll_ctl(_fd,EPOLL_CTL_MOD,fd,&ee)<0)
        {
            Log(ERROR,"epoll modify error\n");
            return false;
        }

        return true;
    }

    bool EpollDel(int fd)
    {
        if(epoll_ctl(_fd,EPOLL_CTL_DEL,fd,nullptr)<0)
        {
            Log(ERROR,"epoll delete error\n");
            return false;
        }

        return true;
    }

    ~Epoll()
    {
        if(_fd>=0)
        {
            close(_fd);
        }
    }

private:
    int _fd;//操作epoll对象的文件描述符
};